package net.xo68.destiny.core.util;

import net.xo68.destiny.core.thread.NamedThreadFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 线程池工具类
 * @author wuxie<mengwzy@qq.com>
 * @version 2020-10-18
 */
public final class ThreadUtils {

    private ThreadUtils(){
        throw new IllegalArgumentException("ThreadPoolUtils not constructor");
    }

    /**
     * 创建调度线程池
     * @param threadPrefix
     * @return
     */
    public static ScheduledExecutorService newSingleThreadScheduled(String threadPrefix){
        return Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory(threadPrefix,true));
    }
    public static ScheduledExecutorService newThreadScheduled(String threadPrefix,int theadCount){
        return Executors.newScheduledThreadPool(theadCount, new NamedThreadFactory(threadPrefix,true));
    }

    /**
     * 创建线程池
     * @param threadPrefix
     * @param theadCount
     * @return
     */
    public static ExecutorService newFixedThreadPool(String threadPrefix,int theadCount){
        return Executors.newFixedThreadPool(theadCount,new NamedThreadFactory(threadPrefix,true));
    }

    /**
     * 创建线程
     * @param threadPrefix
     * @param runnable
     * @return
     */
    public static Thread newThread(String threadPrefix,Runnable runnable){
        return new NamedThreadFactory(threadPrefix).newThread(runnable);
    }
}
